const {
  successResponse,
  errorResponse
} = require('../respMsg')
const utils = require('utility')
const _filter = {
  'pwd': 0,
  '__v': 0
}



const model = require('../model')

const User = model.getModel('user')
const Chat = model.getModel('chat')


function md5Pwd(pwd) {
  const salt = 'sldfkjlsdjflkjwolerjoiwerh98797!'
  return utils.md5(utils.md5(pwd + salt))
}


class UserController {
  // 用户信息
  async info(ctx) {
    const userid = ctx.cookies.get('userid')
    if (!userid) {
      return ctx.body = errorResponse("未登录")
    }
    let res = await User.findOne({
      _id: userid
    }, _filter)
    if (res) {
      return ctx.body = successResponse(res)
    }
  }
  // 用户列表
  async list(ctx) {
    const {
      type
    } = ctx.query
    let res = await User.find({
      type
    })
    return ctx.body = successResponse(res)
  }
  // 注册
  async register(ctx) {
    // let postData = ctx.request.body
    const {
      user,
      pwd,
      type
    } = ctx.request.body
    let username = await User.findOne({
      user
    })
    if (username) {
      return ctx.body = errorResponse('已存在该用户名')
    }
    const UserModel = new User({
      user,
      pwd: md5Pwd(pwd),
      type
    })

    let newUser = await UserModel.save()
    ctx.cookies.set('userid', newUser._id, {
      httpOnly: false
    })
    console.log(newUser)
    return ctx.body = successResponse(newUser)
  }
  // 登录
  async login(ctx) {
    const {
      user,
      pwd
    } = ctx.request.body
    let username = await User.findOne({
      user,
      pwd: md5Pwd(pwd)
    }, _filter)
    if (!username) {
      return ctx.body = errorResponse('用户名或密码错误')
    }
    ctx.cookies.set('userid', username._id, {
      httpOnly: false
    })
    return ctx.body = successResponse(username)
  }
  // 信息更新
  async update(ctx) {
    const userid = ctx.cookies.get('userid')
    if (!userid) return ctx.body = errorResponse("未登录")
    const body = ctx.request.body
    let doc = await User.findByIdAndUpdate(userid, body)
    const data = Object.assign({}, {
      user: doc.user,
      type: doc.type
    }, body)
    return ctx.body = successResponse(data)
  }
  // 获取消息
  async getmsglist(ctx) {
    const user = ctx.cookies.get('userid')
    //
    try {
      let userdoc = await User.find({})
      let users = {}
      userdoc.forEach(v => {
        users[v._id] = {
          name: v.user,
          avatar: v.avatar
        }
      })
      let data= await Chat.find( {'$or':[{from:user},{to:user}]})
      console.log(data)
      return ctx.body={
        success: true,
        data,
        users
      }
    } catch (e) {
      return ctx.body=errorResponse('报错')
    }


  }

  // 已读消息
  async readmsg(ctx){
    const userid = ctx.cookies.get('userid')
    const {from} = ctx.request.body
    console.log(userid,from)
    try{
      const doc= await Chat.update(
        {from,to:userid},
        {'$set':{read:true}},
        {'multi':true}
      )
      console.log(doc)
      return ctx.body=successResponse({num:doc.nModified})
    }catch(e){
      return ctx.body=errorResponse('修改失败')
    }
  }
}

module.exports = new UserController()